import 'package:aero_frisbee/gen/assets.gen.dart';
import 'package:flutter/material.dart';
import 'package:svgaplayer_flutter/parser.dart';
import 'package:svgaplayer_flutter/player.dart';

class AfPayGiftSuccessContentView extends StatefulWidget {
  const AfPayGiftSuccessContentView({Key? key, this.successType = 0})
      : super(key: key);
  final int successType;
  @override
  AfPayGiftSuccessContentViewState createState() =>
      AfPayGiftSuccessContentViewState();
}

class AfPayGiftSuccessContentViewState
    extends State<AfPayGiftSuccessContentView> with TickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<double> _animation;
  SVGAAnimationController? animationController;
  @override
  void initState() {
    super.initState();
    animationController = SVGAAnimationController(vsync: this);
    // 初始化动画控制器
    _animationController = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 1),
    );

    // 创建动画，并添加延迟
    _animation = Tween<double>(
      begin: 0,
      end: 1,
    ).animate(
      CurvedAnimation(
        parent: _animationController,
        curve: Curves.easeIn,
      ),
    );

    // 添加延迟
    Future.delayed(const Duration(seconds: 2), () {
      _animationController.forward();
    });
    _loadAnimation();
  }

  Widget get successImage {
    switch (widget.successType) {
      case 0:
        return Assets.icons.iconGiftsLeft.image();
      case 1:
        return Assets.icons.iconGiftsMid.image();
      default:
        return Assets.icons.iconGiftsRight.image();
    }
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }

  @override
  void dispose() {
    animationController?.dispose();
    _animationController.dispose();
    animationController = null;
    super.dispose();
  }

  void _loadAnimation() async {
    final videoItem =
        await SVGAParser.shared.decodeFromAssets(Assets.svga.success);

    if (mounted) {
      setState(() {
        animationController?.videoItem = videoItem;
        _playAnimation();
      });
    }
  }

  void _playAnimation() {
    if (animationController?.isCompleted == true) {
      animationController?.reset();
    }
    animationController?.forward(); // or animationController.forward();
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 20),
      child: Stack(
        children: [
          SVGAImage(
            animationController!,
            fit: BoxFit.contain,
            clearsAfterStop: false,
            allowDrawingOverflow: true,
            filterQuality: FilterQuality.low,
          ),
          Positioned.fill(
            child: FadeTransition(
              opacity: _animation,
              child: FractionallySizedBox(
                widthFactor: 1,
                heightFactor: 1,
                child: Center(
                  child: LayoutBuilder(
                    builder:
                        (BuildContext context, BoxConstraints constraints) {
                      return successImage;
                    },
                  ),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}
